package com.subway.babynote;
/*水*/
import com.subway.babynote.syncdata.errMessage;
import com.subway.babynote.syncservice.syncapi;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

public class sync extends Activity implements OnClickListener {
	private Button sync_button;
	public NotedbHelper notedb;
	private static final int INVISIBLE = 0x00000004;
	EditText weiboacc_edit;
	EditText weibopasswd_edit;
	TextView hint_view;
	Cursor cur;
	int col;
	Intent In;
	private LinearLayout linearLayout, statistics, sync, more;
	Button login_button;

	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.syncmenu);
		sync_button = (Button) findViewById(R.id.sync_button);
		In = new Intent();
		//initTabFunction();
		notedb = new NotedbHelper(this);
		weiboacc_edit = (EditText) findViewById(R.id.weiboacc_edit);
		weibopasswd_edit = (EditText) findViewById(R.id.weibopasswd_edit);
		login_button = (Button) findViewById(R.id.login_button);
		hint_view = (TextView) findViewById(R.id.hint_view);
		try {
			if (syncapi.getSyncImpl().getUserid() != -1) {
				login_button.setVisibility(INVISIBLE);
				login_button.setClickable(false);
			}
			cur=notedb.getWeibo();
			if(cur!=null&&cur.getCount()>0){		
				//already binded 
				this.startManagingCursor(cur);
				hint_view.setText(getResources().getString(R.string.alreadybinding));
				sync_button.setVisibility(INVISIBLE);
				sync_button.setClickable(false);
				weibopasswd_edit.setVisibility(INVISIBLE);
				//weiboacc_edit.setVisibility(INVISIBLE);
				int col=cur.getColumnIndex("weibo_account");
				if(!cur.isNull(col)){
			    	weiboacc_edit.setText(cur.getString(col));
				}
				weiboacc_edit.setEnabled(false);
				
			}
		} catch (Exception e) {
			Log.v("sync", " e=" + e.getMessage());
		}
		sync_button.setOnClickListener(this);
		login_button.setOnClickListener(this);
	}

	

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		if (v.equals(sync_button)) {
			hint_view.setText("");
			String username = weiboacc_edit.getText().toString();
			String passwd = weibopasswd_edit.getText().toString();
			hint_view.setText(getResources().getString(R.string.validating));
			int iret = syncapi.getSyncImpl().syncWeibo(username, passwd);
			if (iret != 0) {
				hint_view.setText(getResources().getString(R.string.validationfail));
				return;
			} else {
				hint_view.setText(getResources().getString(R.string.bindingsuccess));
				weibopasswd_edit.setEnabled(false);
				weiboacc_edit.setEnabled(false);
				sync_button.setVisibility(INVISIBLE);
				sync_button.setClickable(false);
			}
			// error handling
			int user_id = syncapi.getSyncImpl().getUserid();
			if (user_id == -1) {
				user_id = notedb.getVisitorid();
			}
			//notedb.AddWeibo(user_id, username, passwd);
			// call function to sync weibo
		} else if (v.equals(login_button)) {
			In.setClass(sync.this, login.class);
			startActivity(In);
		}
	}
	
	public void QuitApp() {
		new AlertDialog.Builder(sync.this).setTitle(getResources().getString(R.string.prompt)).setMessage(
				getResources().getString(R.string.confirmMessage)).setIcon(R.drawable.btn_close_normal).setPositiveButton(getResources().getString(R.string.confirm),
				new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int whichButton) {	
					
						finish();
					}
				}).setNegativeButton(getResources().getString(R.string.cancel),
				new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int whichButton) {
					}
				}).show();

	}
	
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		
		switch (keyCode) {
		case KeyEvent.KEYCODE_BACK:
			QuitApp();
			return true;
			
		}
		return false;
	}

	public void onDestroy() {
		super.onDestroy();
		notedb.close();
	}
}